package org.wzl.personalwebsite.service.impl;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wzl.personalwebsite._do.RequestLog;
import org.wzl.personalwebsite.dto.SetLogDTO;
import org.wzl.personalwebsite.mapper.RequestLogMapper;
import org.wzl.personalwebsite.service.LogService;

/**
 * @author 卫志龙
 * @version 1.0
 * @date 2024/9/21 17:56
 */
@Slf4j
@Service
public class LogServiceImpl implements LogService {

    @Resource
    private RequestLogMapper requestLogMapper;

    @Override
    public void setLog(SetLogDTO setLogDTO, HttpServletRequest request) {

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setRequestLog(HttpServletRequest request) {
        // 获取客户端的 IP 地址
        String clientIp = request.getRemoteAddr();

        // 获取请求头中的信息
        String userAgent = request.getHeader("User-Agent");
        // 获取其他与请求相关的信息
        String requestMethod = request.getMethod();
        String requestUri = request.getRequestURI();

        RequestLog requestLog = new RequestLog();
        requestLog.setRequestUri(requestUri);
        requestLog.setClientIp(clientIp);
        requestLog.setUserAgent(userAgent);
//        requestLog.setUserId();

        requestLogMapper.insert(requestLog);
    }

}
